package com.manager.util;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
/*
 * Usage
 * ResultSet rs = null;
 * List list = new Result(rs);
 * 
 * Note:
 * if you user such sql: select name pname from table
 * rsMetaData.getColumnLabel() is work well on mysql
 * rsMetaData.getColumnName() is woek well on oracle
 */
public class Result {

	private ArrayList<LinkedHashMap> result = new ArrayList<LinkedHashMap>();

	public Result() {
	}

	public Result(ResultSet resultSet) throws SQLException {
		if (resultSet != null) {
			ResultSetMetaData rsMetaData = resultSet.getMetaData();

			// 查看返回的列的数据类型
//			 for (int k = 0; k < rsMetaData.getColumnCount(); k++) {
//			 Log.info(rsMetaData.getColumnName(k + 1) + "---" + rsMetaData.getColumnClassName(k + 1));
//			 }

			LinkedHashMap<String, Object> row = null;
			while (resultSet.next()) {
				row = new LinkedHashMap<String, Object>();
				for (int i = 0; i < rsMetaData.getColumnCount(); i++) {
					row.put(rsMetaData.getColumnLabel(i + 1), resultSet.getObject(rsMetaData.getColumnLabel(i + 1)));
//					 Log.info("***>"+resultSet.getObject(rsMetaData.getColumnName(i + 1)).toString());
//					 Log.info("***>"+resultSet.getObject(rsMetaData.getColumnName(i + 1)).getClass().getName());
//					 Log.info("***>"+resultSet.getObject(rsMetaData.getColumnClassName(i + 1)));
				}
				result.add(row);
			}

		}
	}

	public List getList() {
		return result;
	}

}
